﻿using MaterialTypeRecognition.Shell.SaveInfos;
using MaterialTypeRecognition.Shell.SaveInfoService;
using MaterialTypeRecognition.Shell.Utils;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Windows;
using System.Windows.Input;

namespace MaterialTypeRecognition.Shell.Views
{
    /// <summary>
    /// UserManagerWindow.xaml 的交互逻辑
    /// </summary>
    public partial class UserManagerWindow : Window
    {
        private readonly StopwatchTimer _timer;

        private readonly IServiceProvider _services;

        public UserManagerWindow(IServiceProvider services, StopwatchTimer timeUpTimer)
        {
            _services = services;
            _timer = timeUpTimer;
            this.Loaded += UserManagerWindow_Loaded;
            InitializeComponent();
        }

        private int _rfidInterval;

        private void UserManagerWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var service = _services.GetRequiredService<ISaveInfoService>();
            var saveInfo = service.Read<AppSettingSaveInfo>();
            _rfidInterval = saveInfo.RfidInterval!.Value;

            this.RegisterPasswordBox.KeyUp += RegisterPasswordBox_KeyUp;
            this.RegisterPasswordBox.KeyDown += RegisterPasswordBox_KeyDown;

            this.UnregisterPasswordBox.KeyUp += UnregisterPasswordBox_KeyUp;
            this.UnregisterPasswordBox.KeyDown += UnregisterPasswordBox_KeyDown;
        }

        private void UnregisterPasswordBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                // 回车结束输入。
                _timer.Reset();

                this.UnregisterButton.Command?.Execute(this.UnregisterPasswordBox);
                this.UnregisterPasswordBox.Password = string.Empty;
            }
            else
            {
                // 开始计时。
                _timer.Start(TimeSpan.FromMilliseconds(_rfidInterval));
            }
        }

        private void UnregisterPasswordBox_KeyUp(object sender, KeyEventArgs e)
        {
            _timer.Stop();

            if (_timer.IsTimeUp)
            {
                this.UnregisterPasswordBox.Password = string.Empty;
            }
        }

        private void RegisterPasswordBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                // 回车结束输入。
                _timer.Reset();

                this.RegisterButton.Command?.Execute(this.RegisterPasswordBox);
                this.RegisterPasswordBox.Password = string.Empty;
            }
            else
            {
                // 开始计时。
                _timer.Start(TimeSpan.FromMilliseconds(_rfidInterval));
            }
        }

        private void RegisterPasswordBox_KeyUp(object sender, KeyEventArgs e)
        {
            _timer.Stop();

            if (_timer.IsTimeUp)
            {
                this.RegisterPasswordBox.Password = string.Empty;
            }
        }
    }
}
